package com.dehui.traders.ui;

import android.content.Context;
import android.text.TextUtils;

import com.dehui.traders.bean.HistData;
import com.dehui.traders.bean.IndexInfo;
import com.dehui.traders.bean.OptionalCode;
import com.dehui.traders.bean.OptionalCodeRank;
import com.dehui.traders.bean.RealtimeQuote;
import com.dehui.traders.config.Config;
import com.dehui.traders.config.Preferences;
import com.dehui.traders.kchart.EXPMAEntity;
import com.dehui.traders.utils.CacheUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

public class OptionHelper{

    /**
     * 判断本地数据和缓存是否相同code
     * @param optionalCodes
     * @return
     */
    public static boolean isCodeSame(List<OptionalCode> optionalCodes) {
        String myOpitonCode = Preferences.getMyOpitonCode();
        try {
            List<OptionalCodeRank> codeList =new Gson().fromJson(myOpitonCode, new TypeToken<List<OptionalCodeRank>>() {}.getType());
            if (codeList.size() != optionalCodes.size()){
                return false;
            }

            int sameNum = 0;
            for (int i = 0; i < optionalCodes.size(); i++){
                OptionalCode oc = optionalCodes.get(i);
                for (int j = 0 ; j < codeList.size(); j++){
                    OptionalCodeRank cc = codeList.get(j);
                    if (oc.getCode().equals(cc.code)){
                        sameNum++;
                    }
                }
            }

            if (sameNum == optionalCodes.size()){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){

            return false;
        }
    }

    /**
     * 保存自选股数据
     * @param context
     * @param list
     */
    public static void saveRealTimeQuote(Context context, List<RealtimeQuote> list) {
        String json = new Gson().toJson(list);
        CacheUtil cacheUtil = CacheUtil.get(context);
        cacheUtil.put(Config.key_option_stock_list,json);
    }


    /**
     * 获取自选股数据
     * @param context
     * @return
     */
    public static List<RealtimeQuote> getCacheOptionRealTime(Context context) {
        CacheUtil cacheUtil = CacheUtil.get(context);
        String asString = cacheUtil.getAsString(Config.key_option_stock_list);
        try {
            List<RealtimeQuote> list =new Gson().fromJson(asString, new TypeToken<List<RealtimeQuote>>() {}.getType());
            return list;
        }catch (Exception e){

            return null;
        }
    }

    /**
     * 保存kline值
     * @param context
     * @param key
     * @param content
     */
    public static void saveKLine(Context context, String key, String content) {
        CacheUtil cacheUtil = CacheUtil.get(context);
        cacheUtil.put(Config.key_code_k_list + key, content);
    }

    /**
     * 获取kline值
     * @param context
     * @param key
     * @return
     */
    public static List<String[]> getKLine(Context context,String key){
        CacheUtil cacheUtil = CacheUtil.get(context);
        String asString = cacheUtil.getAsString(Config.key_code_k_list + key);
        try {
            List<String[]> list =new Gson().fromJson(asString, new TypeToken<List<String[]>>() {}.getType());
            return list;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 保存K线历史值
     * @param context
     * @param key
     * @param status
     * @param content
     */
    public static void saveKHistLine(Context context,String key,int status,String content){
        CacheUtil cacheUtil = CacheUtil.get(context);
        cacheUtil.put(Config.key_hist_k_list + status + "@" + key,content);
    }

    /**
     * 获取k线历史缓存值
     * @param context
     * @param key
     * @param status
     * @return
     */
    public static List<HistData> getKHist(Context context,String key,int status){
        CacheUtil cacheUtil = CacheUtil.get(context);
        String asString = cacheUtil.getAsString(Config.key_hist_k_list + status + "@" + key);
        try {
            List<HistData> list =new Gson().fromJson(asString, new TypeToken<List<HistData>>() {}.getType());
            return list;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 保存周月k值
     * @param context
     * @param key
     * @param type
     * @param content
     */
    public static void saveZhouKLine(Context context,String key,int type,String content){
        CacheUtil cacheUtil = CacheUtil.get(context);
        cacheUtil.put(Config.key_zhou_k_list + type + "@" + key,content);
    }

    /**
     * 获取周月k值
     * @param context
     * @param key
     * @return
     */
    public static List<String[]> getZhouKLint(Context context,String key,int type){
        CacheUtil cacheUtil = CacheUtil.get(context);
        String asString = cacheUtil.getAsString(Config.key_zhou_k_list + type + "@" + key);
        try {
            List<String[]> list =new Gson().fromJson(asString, new TypeToken<List<String[]>>() {}.getType());
            return list;
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 保存首页指数数据
     * @param context
     * @param content
     */
    public static void saveHomeZhishu(Context context,String content){
        CacheUtil cacheUtil = CacheUtil.get(context);
        cacheUtil.put(Config.key_home_zhishu,content);
    }

    /**
     * 获取首页指数数据
     * @param context
     * @return
     */
    public static List<IndexInfo> getHomeZhishu(Context context){
        CacheUtil cacheUtil = CacheUtil.get(context);
        String asString = cacheUtil.getAsString(Config.key_home_zhishu);
        try {
            List<IndexInfo> list =new Gson().fromJson(asString, new TypeToken<List<IndexInfo>>() {}.getType());
            return list;
        }catch (Exception e){
            return null;
        }
    }


}
